<?php
// +----------------------------------------------------------------------
// | ThinkPHP
// +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
// 库存产品

class GoodsAction extends CommonAction {
	
	function _filter(&$map){
		$map['product_name'] = array('like',"%".$_REQUEST['key']."%");
	}
	
	
	public function _before_add(){
		$this->assign('category',getGoodsCategory());
		$this->assign('brand',getGoodsBrand());		
		$this->assign('unit',getUnit());
	}

	public function _before_edit(){
		$this->assign('category',getGoodsCategory());
		$this->assign('brand',getGoodsBrand());
		$this->assign('unit',getUnit());
	}
	
	public function _before_insert(){
		/*
		新增产品信息前的操作：上传图片
		*/
		if($_FILES['image']['size' ] > 0){
		import("ORG.Net.UploadFile");
		$upload = new UploadFile();
		$upload->maxSize  = 3145728 ;
		$upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
		$upload->savePath =  './Public/Uploads/';
		if(!$upload->upload()) {
			$this->error($upload->getErrorMsg());
		}else{
			$info =  $upload->getUploadFileInfo();
		 }
		//dump($info);die;
			$_POST['image']=$info[0]['savepath'].$info[0]['savename'];
		}
	}
	
	public function insert(){
        $model = D($this->getActionName());
        if (false === $model->create()) {
            $this->error($model->getError());
        }
        //保存当前数据对象
        if ($result = $model->add()) { //保存成功
			$vo=$model->find($result);
			$vo=gen_goods_code($vo);
			$model->save($vo);
            //成功提示
            $this->assign('jumpUrl', Cookie::get('_currentUrl_'));
            $this->dwzAjaxResponse(200,'添加成功！','Goods');
		   // $this->success(L('新增成功'));
        } else {
            //失败提示
            //$this->error(L('新增失败'));
            $this->dwzAjaxResponse(300,'添加失败！');
		}
	} 
	
/* 	public function  _tigger_insert($model){
		//生成产品编号 生成条形码
		dump($model);
		dump($model->data);
		$id=$model->data;
		$id=$id['id'];
		$vo=$model->where($id)->find();
		$vo=gen_goods_code($vo);
		var_dump($vo);

		/**
		更新产品编号 生成条形码
		
		$model->save($vo);		die;
	} */
	
	
	public function add_cart(){
		$ids = explode("_",$_REQUEST['ids']);
		$cart=session('goods_ids');
		//dump($cart);
		if(empty($cart)){
			$cart = $ids;
		}else{
			$cart=array_merge($cart,array_diff($ids,$cart));
		}
		//dump($cart);
		session('goods_ids',$cart);
		//echo "<script>window.close();</script>";
		/**
		之后加上ajax return
		*/
		
	}
	
	/**
	显示添加到cart里的产品 并显示相关操作
	*/
	public function view_cart(){
		$cart=session('goods_ids');
		$condition="";
		foreach($cart as $val){
			$condition.=",".$val;
		}
		$condition="id in (".substr($condition,1).")";
		$Goods=M('Goods');
		//$list=$Goods->where($condition)->getField('id,product_code,product_name,price');
		$list=$Goods->where($condition)->select();
		//dump($list);
		$this->assign('list',$list);
		$this->display();
	}
	
	/**
	ajax 返回搜索结果 json数据格式
	*/
	
	public function ajax_search(){
		$Goods=M('Goods');
		$condition='';
		$key =$_REQUEST['key'];
		$list=$Goods->where("product_code like '%".$key."%' OR product_name like '%".$key."%'")->select();
		$this->ajaxReturn($list,'返回成功',1);
	}
	
	public function search(){
		$Goods=M('Goods');
		$condition='';
		$key =$_REQUEST['key'];
		$list=$Goods->where("product_code like '%".$key."%' OR product_name like '%".$key."%'")->select();
		foreach($list as $key=>$vo){
			$list[$key]['html']=urlencode(create_purchase($vo));
		}
		$this->assign('list',$list);
		$this->display();
	}
	
	
	/**
	根据产品编号返回json产品数据
	*/
	public function test(){
	$this->display();
	}
	
	public function ajax_info(){
		$Goods=M('Goods');
		$product_code=$_REQUEST['product_code'];
		$info=$Goods->where("product_code=".$product_code)->field('id,product_code,product_name,brand,unit,inventory_number,purchase_price')->find();
		if($info['id']>0){
			$this->ajaxReturn($info,'返回成功',1);
		}else{
			$this->ajaxReturn($product_code,'未查询到数据',0);
		}
	}
}